package com.xiaoyu.base.serverpush.event;

import com.xiaoyu.base.serverpush.EventCompleteListener;
import in.srain.cube.request.JsonData;

public abstract class ServerPushBaseEvent {

    public final String eventId;
    public final String eventKey;
    public final boolean needReceipt;
    public final String timestampKey;
    public final long timestamp;
    private EventCompleteListener mEventCompleteListener;

    public ServerPushBaseEvent(JsonData jsonData) {
        eventId = jsonData.optString("id");
        eventKey = jsonData.optString("key");
        needReceipt = jsonData.optBoolean("needReceipt");
        timestampKey = jsonData.optString("tsKey");
        timestamp = jsonData.optLong("ts");
    }

    protected abstract void takeAction();

    public final void setComplete() {
        if (mEventCompleteListener != null) {
            mEventCompleteListener.onComplete(this);
        }
    }

    public void setEventCompleteListener(EventCompleteListener eventCompleteListener) {
        mEventCompleteListener = eventCompleteListener;
    }

    public void execute() {
        takeAction();
    }

    @Override
    public String toString() {
        return "ServerPushBaseEvent{" +
                "eventId='" + eventId + '\'' +
                ", eventKey='" + eventKey + '\'' +
                ", needReceipt=" + needReceipt +
                '}';
    }
}
